package com.tianxinweb.utils;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取真实来源IP
 * @author cboy
 *
 */
public class IpUtil {
	

	public static String getIp(HttpServletRequest request) {
	    String ip = request.getHeader("X-Real-IP");
	    
	    if (ip != null && !"unknown".equalsIgnoreCase(ip)) {
	        return ip;
	    }
	    ip = request.getHeader("X-Forwarded-For");

	    if (ip != null && !"unknown".equalsIgnoreCase(ip)) {
	        int index = ip.indexOf(",");
	        if (index != -1) {
	            return ip.substring(0, index);
	        } else {
	            return ip;
	        }
	    } else {
			ip = request.getRemoteAddr();
			if("0:0:0:0:0:0:0:1".equals(ip)) return "127.0.0.1";
	        return ip;
	    }
	}
	
}
